package array;

/**
 * 二维数组动态初始化
 * 类型[][] 数组名 = new 类型[大小][大小]
 *
 * 二维数组静态初始化（类似一维数组直接赋值）
 * 类型[][] 数组名 = {{1, 2, 3}, {4}}
 */
public class TwoDimensionalArray2 {

    public static void main(String[] args) {

        int[][] arr;//先声明
        arr = new int[2][3];//开辟空间
        arr[1][1] = 8;//赋值

        //遍历
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }

        /*
             int arr[][] = {{4,6},{1,4,5,7},{-2}};遍历该二维数组，并得到和
         */
        int[][] arr2 = {{4, 6}, {1, 4, 5, 7}, {-2}};
        int sum = 0;
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                sum += arr2[i][j];
            }
        }
        System.out.println("二维数组各元素求和=" + sum);
    }
}
